package com.flw.eslab.service.impl;

import java.io.File;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.flw.eslab.dao.RoleDao;
import com.flw.eslab.dao.UserDao;
import com.flw.eslab.dto.PageInfo_Dto;
import com.flw.eslab.model.Group;
import com.flw.eslab.model.Role;
import com.flw.eslab.service.RoleManager;

@Service("RoleManager")
public class RoleManagerImpl implements RoleManager {
	@Resource
	private RoleDao roleDao;
	@Resource
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public boolean add(Role role) {
		if(role.getName()!=null){
			if(roleDao.findByHql("from Role where name="+role.getName())==null){
				roleDao.save(role);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean delete(Role role) {
		if(role.getId()==null) return false;		
		if(userDao.findByRole(role)==null){
			roleDao.deleteById(Role.class, role.getId());
			return true;
		}
		return false;
	}

	@Override
	public boolean modify(Role role) {
		if(role.getId()==null) return false;
		if(roleDao.findById(Role.class, role.getId())!=null){
			if(role.getName()!=null){
				if(roleDao.findByHql("from Role where name="+role.getName())==null){
					roleDao.update(role);
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public File output() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageInfo_Dto load(PageInfo_Dto pageInfo_Dto) {
		int pageNow=pageInfo_Dto.getPageNow()==null?1:pageInfo_Dto.getPageNow();
		int pageSize=pageInfo_Dto.getPagesize()==null?5:pageInfo_Dto.getPagesize();	
		return roleDao.findByPageInfo(Role.class, pageNow, pageSize, null, null);
	}

}
